Programming ASP.NET MVC 4 by Jess Chadwick Todd Snyder & Hrusikesh Panda

Programming ASP.NET MVC 4 by Jess Chadwick Todd Snyder & Hrusikesh Panda

Author:Jess Chadwick, Todd Snyder & Hrusikesh Panda [Jess Chadwick, Todd Snyder, and Hrusikesh Panda]
Language: eng
Format: epub
Tags: COMPUTERS / Web / Web Programming
ISBN: 9781449320300
Publisher: O'Reilly Media
Published: 2012-09-13T16:00:00+00:00


Donut Hole Caching

Donut hole caching is the inverse of donut caching: while the donut caching technique caches the entire page, leaving out only a few small sections, donut hole caching caches only one or a few portions of the page (the donut “holes”).

For example, the Ebuy reference application contains a list of auction categories that do not change often, so it makes sense to render all of the categories just once and cache the resulting HTML.

Donut hole caching is very useful in these kinds of scenarios, where most of the elements in your page are dynamic, with the exception of a few sections that rarely change, or are changed based on a request parameter. And, unlike donut caching, ASP.NET MVC has great support for donut hole caching through the use of child actions.

Let’s see donut hole caching in action by applying it to the Ebuy auction categories example mentioned above. Here is the partial view that we will be caching:

@{ Layout = null; } <ul> @foreach(var category in ViewBag.Categories as IEnumerable<Category>) <li>@Html.ActionLink(@category.Name, "category", "categories", new { categoryId = category.Id })</li> </ul>

This partial view enumerates through all the categories and renders each one as a list item in an unordered list. Each item in the list is a link to the Category action in the Categories controller, passing the category’s Id as an action parameter.

Next, create a child action that displays this view:

[ChildActionOnly] [OutputCache(Duration=60)] public ActionResult CategoriesChildAction() { // Fetch Categories from the database and // pass it to the child view via its ViewBag ViewBag.Categories = Model.GetCategories(); return View(); }

Notice how the OutputCacheAttribute caches the result of this method for 60 seconds.

Then you can call this new action from a parent view by calling @Html.Action("CategoriesChildAction"), as shown in the following example:

<header> <h1>MVC Donut Hole Caching Demo</h1> </header> <aside> <section id="categories"> @Html.Action("CategoriesChildAction") </section> </aside> <!-- Rest of the page with non cacheable content goes here -->

Now when the page is rendered, the Categories child action is called to generate the list of categories.

The results of this call get cached via the OutputCacheAttribute, so when the page is rendered the next time, the Categories list is rendered from the cache while the rest of the page is generated from scratch.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.